package com.naver.java3d.panorama.test;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class House implements ImageProducer {
	int rx, ry;
	int _width;
	int _height;
	Image _image;
	ImageConsumer _consumer;
	DirectColorModel _model;

	static Z3D_Scene scene;

	public House() {
		int l = 512;
		Dimension size = new Dimension(l, l);
		_width = size.width;
		_height = size.height;
		_model = new DirectColorModel(32, 0x00FF0000, 0x000FF00, 0x000000FF, 0);
		_image = Toolkit.getDefaultToolkit().createImage(this);
	}

	public static void main(String[] args) {
		House house = new House();
		
		long t1 = System.currentTimeMillis();
		house.render();
		System.out.println(System.currentTimeMillis() - t1);
	}

	private void render() {
		int size = _width * _height;
		int buf[] = new int[size];

		scene = new Z3D_Scene(buf, _width, _height);

		scene.addRotation(0, 20);
		scene.render();
		update(buf);

		scene.addRotation(100, 200);
		scene.render();
		update(buf);
	}

	public void update(Object pixels) {
		if (_consumer != null) {
			_consumer.setPixels(0, 0, _width, _height, _model, (int[])pixels, 0, _width);

			_consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
		}

		paint();
	}

	private void paint() {
		BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
		Graphics g1 = image.getGraphics();
		g1.drawImage(_image, 0, 0, _width, _height, null);
		try {
			ImageIO.write(image, "JPEG", new File("d:/sample.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addConsumer(ImageConsumer ic) {
		_consumer = ic;
		_consumer.setDimensions(_width, _height);
		_consumer.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES | ImageConsumer.SINGLEPASS | ImageConsumer.SINGLEFRAME);
		_consumer.setColorModel(_model);
	}

	public boolean isConsumer(ImageConsumer ic) {
		return true;
	}

	public void removeConsumer(ImageConsumer ic) {
	}

	public void startProduction(ImageConsumer ic) {
		addConsumer(ic);
	}

	public void requestTopDownLeftRightResend(ImageConsumer ic) {
	}
}